<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生信息管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1>学生信息管理系统</h1>
            <div>
                <span>欢迎, {{ user.username }}!</span>
                <a href="{{ url_for('logout') }}" class="btn btn-outline-secondary ms-2">注销</a>
            </div>
        </div>
        
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="alert alert-{{ category }}">{{ message }}</div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        {% if user.is_admin %}
        <a href="{{ url_for('add_student') }}" class="btn btn-primary mb-3">添加学生</a>
        {% endif %}
        
        <form action="{{ url_for('search_student') }}" method="get" class="mb-3">
            <div class="input-group">
                <input type="text" name="query" class="form-control" placeholder="搜索学生..." value="{{ search_query or '' }}">
                <button type="submit" class="btn btn-outline-secondary">搜索</button>
            </div>
        </form>

        {% if search_query %}
        <p>搜索结果: "{{ search_query }}"</p>
        {% endif %}

        <table class="table table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>年级</th>
                    {% if user.is_admin %}
                    <th>操作</th>
                    {% endif %}
                </tr>
            </thead>
            <tbody>
                {% if students %}
                    {% for student in students %}
                    <tr>
                        <td>{{ student.id }}</td>
                        <td>{{ student.name }}</td>
                        <td>{{ student.age }}</td>
                        <td>{{ student.grade }}</td>
                        {% if user.is_admin %}
                        <td>
                            <a href="{{ url_for('edit_student', id=student.id) }}" class="btn btn-sm btn-warning">编辑</a>
                            <a href="{{ url_for('delete_student', id=student.id) }}" class="btn btn-sm btn-danger" onclick="return confirm('确定要删除这个学生吗?')">删除</a>
                        </td>
                        {% endif %}
                    </tr>
                    {% endfor %}
                {% else %}
                    <tr>
                        <td colspan="{% if user.is_admin %}5{% else %}4{% endif %}" class="text-center">没有找到匹配的学生</td>
                    </tr>
                {% endif %}
            </tbody>
        </table>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>